﻿using System.ServiceModel;
using System.Windows.Controls;
using System.Windows;
using Caliburn.Micro;
using PicturePortfolio.ServiceReference;
using PicturePortfolio.Silverlight.Message;
using PicturePortfolio.Silverlight.Extension;
namespace PicturePortfolio.Silverlight.ViewModels
{
    public class PortfolioContainerViewModel:Screen,IHandle<ImageSelected>,IHandle<RefreshPhotoPreviewMessage>,IHandle<ErrorMessage>,IHandle<CategoryAddedOrDeleted>
    {
        PhotoServiceClient _photoServiceClient = null;
        public PortfolioContainerViewModel()
        {
            _photoServiceClient = new PhotoServiceClient();
            this.Activescreen = new ThumbnailViewModel(_photoServiceClient);
            this.CategoriesView = new CategoryListViewModel(_photoServiceClient);
            EventAggregationProvider.EventAggregator.Subscribe(this);

            if (AccessPermission.IsLoggedInUser == true)
                this.IsShowAdminFunctionality = Visibility.Visible;
            else
                this.IsShowAdminFunctionality = Visibility.Collapsed;
        }

        private Visibility _isShowAdminFunctionality;
        public Visibility IsShowAdminFunctionality
        {
            get { return _isShowAdminFunctionality; }
            set
            {
                _isShowAdminFunctionality = value;
                NotifyOfPropertyChange(() => IsShowAdminFunctionality);
            }
        }
        private object _categoriesView;
        public object CategoriesView
        {
            get { return _categoriesView; }
            set
            {
                _categoriesView = value;
                NotifyOfPropertyChange(() => CategoriesView);
            }
        }
        private object _activeScreen;
        public object Activescreen
        {
            get { return _activeScreen; }
            private set
            {
                _activeScreen = value;
                NotifyOfPropertyChange(() => Activescreen);
            }
        }

        private object _previewImage;
        public object PreviewImage
        {
            get { return _previewImage; }
            private set 
            {
                _previewImage = value;
                NotifyOfPropertyChange(() => PreviewImage);
            }
        }

        private bool _isPreivewOn;
        public bool PreivewOn
        {
            get { return _isPreivewOn; }
            set
            {
                _isPreivewOn = value;
                NotifyOfPropertyChange(() => PreivewOn);
            }
        }
        public void UploadImage()
        {
            UploadPhotoViewModel uploaViewModel=new UploadPhotoViewModel(_photoServiceClient);
            IWindowManager popuWindowManager = new PopupWindowManager();
            popuWindowManager.ShowDialog(uploaViewModel, null);
        }
        public void ShowAllImage()
        {
            this.Activescreen = new ThumbnailViewModel(_photoServiceClient);
        }
        public void CreateNewCategory()
        {
            if (_popupManager == null)
                _popupManager = new PopupWindowManager();
            AddEditCategoryViewModel categoryList = new AddEditCategoryViewModel(_photoServiceClient);
            _popupManager.ShowDialog(categoryList);
        }
        PopupWindowManager _popupManager = null;
        public void Handle(ImageSelected message)
        {
            PreviewPhotoViewModel preview = new PreviewPhotoViewModel(_photoServiceClient, message.SelectedImage);
            if(_popupManager==null)
                _popupManager = new PopupWindowManager();

            _popupManager.ShowDialog(preview);
        }

        public void Handle(RefreshPhotoPreviewMessage message)
        {
            if(message.Sender.GetType()==typeof(CategoryListViewModel))
            {
                this.Activescreen = new ThumbnailViewModel(_photoServiceClient, message.SelectedCategory);
            }
            else
            {
                this.Activescreen = new ThumbnailViewModel(_photoServiceClient);
            }
        }

        public void Handle(ErrorMessage message)
        {
            UnhandledExceptionViewModel exceptionViewModel = new UnhandledExceptionViewModel(message.Exception);
            if (_popupManager == null)
                _popupManager = new PopupWindowManager();

            _popupManager.ShowDialog(exceptionViewModel);
        }

        public void Handle(CategoryAddedOrDeleted message)
        {
            this.CategoriesView = new CategoryListViewModel(_photoServiceClient);
        }
    }
}
